package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import org.example.entity.Book;
import org.example.excel.BookExcel;
import org.example.excel.BookExcelReadListener;
import org.example.exception.ErrorEnum;
import org.example.exception.ServiceException;
import org.example.mapper.BookMapper;
import org.example.param.BookParam;
import org.example.service.BookService;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    BookMapper bookMapper;

    @Override
    public Map<String, Object> save(Book material) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", material.getName());
        if (bookMapper.exists(queryWrapper)) {
            throw new ServiceException(ErrorEnum.BOOK_EXISTS);
        }
        bookMapper.insert(material);
        return success();
    }

    @Override
    public Map<String, Object> page(BookParam param) {
        IPage<Book> page = new Page<>(param.getCurrent(), param.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isNullOrEmpty(param.getBookName())) {
            queryWrapper.like("name", param.getBookName());
        }
        if (param.getLowStock() == null && param.getHighStock() != null) {
            queryWrapper.le("stock", param.getHighStock());
        }
        if (param.getLowStock() != null && param.getHighStock() == null) {
            queryWrapper.ge("stock", param.getLowStock());
        }
        if (param.getLowStock() != null && param.getHighStock() != null) {
            queryWrapper.between("stock", param.getLowStock(), param.getHighStock());
        }
        page =  bookMapper.selectPage(page, queryWrapper);
        return success(page);
    }

    @Override
    public Map<String, Object> disable(Book book) {
        bookMapper.updateById(book);
        return success();
    }

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> importExcel(MultipartFile file) {
        BookExcelReadListener listener = new BookExcelReadListener();
        try {
            EasyExcel.read(file.getInputStream(), BookExcel.class, listener).sheet().doRead();
            List<BookExcel> list = listener.getList();
            String sql = "insert into book (name, stock,warning_stock) values (?,?,?)";
            jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    BookExcel book = list.get(i);
                    ps.setObject(1, book.getName());
                    ps.setObject(2, book.getStock());
                    ps.setObject(3, book.getWarningStock());
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
            return success();
        } catch (IOException e) {
            throw new ServiceException(ErrorEnum.EXCEL_IMPORT_ERROR);
        }
    }
}
